// Flutter Packages
import 'package:flutter/material.dart';

// This Package
import 'package:cached_video_player_plus/cached_video_player_plus.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Cached Video Player Plus Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Cached Video Player Plus Example'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late CachedVideoPlayerPlusController controller;

  @override
  void initState() {
    super.initState();
    controller = CachedVideoPlayerPlusController.networkUrl(
      // https://www.runoob.com/try/demo_source/mov_bbb.mp4
      // https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4
      Uri.parse(
        'https://www.runoob.com/try/demo_source/mov_bbb.mp4',
      ),
      httpHeaders: {
        'Connection': 'keep-alive',
      },
      invalidateCacheIfOlderThan: const Duration(minutes: 10),
    )..initialize().then((value) async {
        await controller.setLooping(true);
        controller.play();
        setState(() {});
      });
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  void _clearCache() async {
    try {
      await CachedVideoPlayerPlusController.clearAllCache();
    } catch (e) {
      print('clearAllCache err:${e.toString()}');
    }
  }

  void _removeCurrentVideoFromCache() async {
    try {
      await controller.removeCurrentFileFromCache();
    } catch (e) {
      print('removeCurrentFileFromCache err:${e.toString()}');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        children: [
          Expanded(
            child: Center(
              child: controller.value.isInitialized
                  ? AspectRatio(
                      aspectRatio: controller.value.aspectRatio,
                      child: CachedVideoPlayerPlus(controller),
                    )
                  : const CircularProgressIndicator.adaptive(),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: ElevatedButton(
              onPressed: _removeCurrentVideoFromCache,
              child: const Text('Remove Current Video from Cache'),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: ElevatedButton(
              onPressed: _clearCache,
              child: const Text('Clear All Cache'),
            ),
          ),
        ],
      ),
    );
  }
}